package com.lhd._05weibo.controller;

import com.lhd._05weibo.mapper.UserMapper;
import com.lhd._05weibo.pojo.dto.UserLoginDTO;
import com.lhd._05weibo.pojo.dto.UserRegDTO;
import com.lhd._05weibo.pojo.entity.User;
import com.lhd._05weibo.pojo.vo.UserVO;
import com.lhd._05weibo.common.response.JsonResult;
import com.lhd._05weibo.common.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author lhd
 * @date 2024/4/23
 * @apiNote
 */
@Api(tags = "01.用户模块")
@Slf4j
@RestController()
@RequestMapping("/v1/users/")
/**
 * Slf4j注解
 *   1.日志注解,添加在类上;
 *   2.在类上添加此注解后,会自动创建一个日志对象叫做log;
 * Knife4j注解：
 *   1.Api注解：设置控制器类名在Knife4j文档中显示的名称;
 *   2.ApiOperation注解：设置控制器方法名在Knife4j文档中显示的名称;
 *   3.ApiIgnore注解：Knife4j忽略此参数,不会提现到Knife4j的文档中;
 *   4.ApiModelProperty注解:
 *     4.1 添加在DTO和VO类属性上;
 *     4.2 用于调整Knife4j文档中的参数说明及描述等;
 *   5.ApiImplicitParam注解
 *     ApiImplicitParams注解
 *     对声明参数方式接收的参数进行描述;
 */
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     *   * ResponseBody注解: Java对象 ------>  JSON格式  ------> Web前端;
     *   * RequestBody注解:  JS对象   ------>  JSON格式  ------> Java服务端;
     */
    @ApiOperation(value ="注册功能")
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO) {
        //以日志的形式打印输出
        log.debug("userRegDTO = " + userRegDTO);
        /*
        1.接收参数[已完成]
            2.数据合法性校验[暂时不做，后续用验证框架做]
            3.校验用户名是否被占用
                3.1 被占用 return 2
                3.2 未被占用 【insert接口】,return 1
         */
        //校验用户名是否被占用
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        //被占用
        if (userVO != null){
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXIST);
        }
        //未被占用
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreated(new Date());
        userMapper.insert(user);
        //注册成功
        return JsonResult.ok();
    }

    /**
     * 2.登录功能
     */
    @ApiOperation(value ="登录功能")
    @PostMapping("login")
    @ApiImplicitParam(name="userLoginDTO",value = "UserLoginDTO",required = true,dataType = "UserLoginDTO")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession httpSession) {
        //登录功能日志输出
        log.debug("userLoginDTO = " + userLoginDTO);
        /*
            1.根据用户名查询;
            2.未查到数据,用户名错误;
            3.查到了数据,比较密码是否正确;
         */
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        //查询 没查到数据 说明用户不存在 用户名错误
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }

        //比较密码
        if(userVO.getPassword().equals(userLoginDTO.getPassword())){
            //设置唯一标识,用于会话保持 将
            httpSession.setAttribute("user", userVO);
            return new JsonResult(StatusCode.LOGIN_SUCCESS);
        }
        //密码错误
        return new JsonResult(StatusCode.PASSWORD_ERROR);
    }

    /**
     * 3.获取当前用户功能
     */
    @ApiOperation(value ="获取当前用户")
    @GetMapping(value = "currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession httpSession) {

        //通过session会话保持技术获取到当前user
        UserVO userVO = (UserVO)httpSession.getAttribute("user");
        log.debug("userVO = {}",userVO);
        return JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录功能
     */
    @ApiOperation(value ="退出登录")
    @GetMapping(value = "logout")
    public void logout(@ApiIgnore HttpSession httpSession) {
        //删除服务器端的会话唯一标识
        httpSession.removeAttribute("user");
    }

}
